/* Copyright (c) 2011-2012 ISP RAS, Moscow. For more information please write to
 
      mermakov@ispras.ru
      svartanov@ispras.ru

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package ru.ispras.coffeemachine.instrument;

import org.apache.bcel.generic.ALOAD;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.DUP;
import org.apache.bcel.generic.GETSTATIC;
import org.apache.bcel.generic.INVOKESPECIAL;
import org.apache.bcel.generic.INVOKEVIRTUAL;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.NEW;
import org.apache.bcel.generic.PUSH;

import ru.ispras.coffeemachine.util.Options;

/**
 * Coffee Machine, instrument for dynamic analysis Java-programs
 * 
 * @author Mikhail Ermakov (mermakov@ispras.ru)
 * @version 1.2.0
 */
public class ConsoleInstructionFactory extends CoffeeInstructionFactory 
{
	private static final long serialVersionUID = 1L;

	public ConsoleInstructionFactory(ConstantPoolGen cp)
	{
		super(cp);
		invokePrintMethod = cp.addMethodref("java.io.PrintStream", "println", "(Ljava/lang/String;)V"); 
	}
	
	public ConsoleInstructionFactory(ClassGen cg, ConstantPoolGen cp)
	{
		super(cg, cp);
		invokePrintMethod = cp.addMethodref("java.io.PrintStream", "println", "(Ljava/lang/String;)V");
	}

	@Override
	public InstructionList createPrintln(String string) 
	{
		InstructionList il = new InstructionList();
		int out = cp.addFieldref("java.lang.System", "out", "Ljava/io/PrintStream;");
		

		il.append(new GETSTATIC(out));
		if (Options.tracePrefix != null) {
			il.append(new PUSH(cp, Options.tracePrefix + string));
		} else {
			il.append(new PUSH(cp, string));
		}
		il.append(new INVOKEVIRTUAL(invokePrintMethod));

		return il;

	}
	
	@Override
	public InstructionList createDaemonPrintln()
	{
		InstructionList il = new InstructionList();
		
		int out = cp.addFieldref("ru.ispras.coffeemachine.target.Daemon", "out", "Ljava/io/PrintWriter;");
		int println = cp.addMethodref("java.io.PrintWriter", "println", "(Ljava/lang/String;)V");
		
		il.append(new GETSTATIC(out));
		
		if (Options.tracePrefix != null) 
		{
			il.append(new NEW(cp.addClass("java/lang/StringBuilder")));
			il.append(new DUP());
			il.append(new INVOKESPECIAL(cp.addMethodref("java/lang/StringBuilder", "<init>", "()V")));
			il.append(new PUSH(cp, Options.tracePrefix));
			int append = cp.addMethodref("java/lang/StringBuilder", "append", 
                    						"(Ljava/lang/String;)Ljava/lang/StringBuilder;");
			il.append(new INVOKEVIRTUAL(append));
			/* Since this is a static method, param is in the first register */
			il.append(new ALOAD(0));
			il.append(new INVOKEVIRTUAL(append));
			il.append(new INVOKEVIRTUAL(cp.addMethodref("java/lang/StringBuilder", "toString", "()Ljava/lang/String;")));
		}
		else
		{
			/* Since this is a static method, param is in the first register */
			il.append(new ALOAD(0));
		}
		il.append(new INVOKEVIRTUAL(println));
		
		return il;
	}
}
